(function($) {
    'use strict';


    $(document).ready(function() {
        var kindvalue = 2;
        $("#kindselect").change(function(){
            kindvalue= $('#kindselect option:selected').val();
            getlist(kindvalue);
        });
        $(".searchBtn").click(function () {
            var searchvalue =  $(".searchvalue").val();
            if (!searchvalue){
                getlist(kindvalue);
            }else {
               $.ajax({
                    type: "get",
                    url: "/api/bookingrecord/phonenumberlist",    //向后端请求数据的url
                    data: {
                        phonenumber:searchvalue
                    },
                    success: function (data) {
                        if (data){
                            if (data.length>0){
                                $(".searchvalue").val("");
                                $(".tbody").empty();
                                for(var i=0;i<data.length;i++){
                                    var dom =' <tr class='+"booking_"+data[i].id+'>\n' +
                                        '<td>'+data[i].customer.name+'</td>\n' +
                                        '<td>'+data[i].customer.phoneNumber+'</td>\n' +
                                        '<td>'+data[i].peopleNum+'人</td>\n' +
                                        '<td>'+data[i].tableNo+'号桌</td>\n' ;
                                    if (data[i].isArrival=="0" && data[i].recordCancle=="0" && data[i].isOver=="0"){
                                        dom+='<td class="am-hide-sm-only">已预约</td>';
                                    }else if (data[i].isArrival=="1"){
                                        dom+='<td class="am-hide-sm-only">就餐中</td>';
                                    }else if(data[i].recordCancle=="1"){
                                        dom+='<td class="am-hide-sm-only">已取消</td>';
                                    }
                                    else if(data[i].isOver=="1"){
                                        dom+='<td class="am-hide-sm-only">已完成</td>';
                                    }
                                    dom+='<td class="am-hide-sm-only">'+data[i].arrivalTime+'</td>\n' +
                                        '<td>\n' +
                                        '<div class="am-btn-toolbar">\n' +
                                        '<div class="am-btn-group am-btn-group-xs">\n';
                                    if (data[i].recordCancle==="0" && data[i].isArrival==="0"  && data[i].isOver=="0"){
                                        dom +='<button class="am-btn am-btn-default am-btn-xs am-text-warning lxy_start"><span class="am-icon-lock"></span> 入座</button>';
                                        dom +='<button class="am-btn am-btn-default am-btn-xs am-text-secondary lxy_edit"><span class="am-icon-pencil-square-o"></span> 编辑</button>';
                                        dom += '<button class="am-btn am-btn-default am-btn-xs am-text-danger  lxy_delete"><span class="am-icon-trash-o"></span> 取消</button>\n';
                                    }else if (data[i].isArrival=="1"){
                                        dom +='<button class="am-btn am-btn-default am-btn-xs am-text-success lxy_over"><span class="am-icon-check"></span> 结束</button>';
                                    }
                                    dom+=  '</div>\n' +
                                        '</div>\n' +
                                        '</td>\n' +
                                        '</tr>';
                                    $(".tbody").append(dom);
                                }
                                deletefunc();
                                editfunc();
                                finishfunc();
                                startfunc();
                            }else {
                                $.alert("未查询到预约记录！");
                                $(".searchvalue").val("");
                            }


                        }else {
                            $.alert("系统超时，获取失败！");
                        }
                    }
                });
            }


        });
        $(".peoplenumber").keyup(function(){
          var  number = $(".peoplenumber").val();
            if (number<=0){
                $.alert("就餐人数至少为1人");
            }
            if (number>0 && number<=4){
                number=4;
            }
            if (number>4 && number<=8){
                number=8;
            }
            if (number>8){
                number=12;
            }
            if (number!=""){
                $.ajax({
                    type: "get",
                    url: "/api/table/valid",    //向后端请求数据的url
                    data: {
                        peoplenum:number
                    },
                    success: function (data) {
                        $("#tableselect").empty();
                        console.log(data)
                        if (data.length>0){
                            for(var i=0;i<data.length;i++){
                                var dom='  <option value="'+data[i].tableNo+'">'+data[i].tableNo+"号餐桌"+'</option>';
                                $("#tableselect").append(dom);
                            }
                        }else {
                            $.alert("暂无可用餐桌");

                        }


                    }
                });
            }


        });

        getlist(kindvalue);

    function getlist(kind) {
        $.ajax({
            type: "get",
            url: "/api/bookingrecord/alllist",    //向后端请求数据的url
            data: {
                kind:kind
            },
            success: function (data) {
                console.log(data);
                if(data){
                    $(".tbody").empty();
                    for(var i=0;i<data.length;i++){
                        var dom =' <tr class='+"booking_"+data[i].id+'>\n' +
                            '<td>'+data[i].customer.name+'</td>\n' +
                            '<td>'+data[i].customer.phoneNumber+'</td>\n' +
                            '<td>'+data[i].peopleNum+'人</td>\n' +
                            '<td>'+data[i].tableNo+'号桌</td>\n' ;
                        if (data[i].isArrival=="0" && data[i].recordCancle=="0" && data[i].isOver=="0" ){
                            dom+='<td class="am-hide-sm-only">已预约</td>';
                        }else if (data[i].isArrival=="1"){
                            dom+='<td class="am-hide-sm-only">就餐中</td>';
                        }else if(data[i].recordCancle=="1"){
                            dom+='<td class="am-hide-sm-only">已取消</td>';
                        }
                        else if(data[i].isOver=="1"){
                            dom+='<td class="am-hide-sm-only">已完成</td>';
                        }
                            dom+='<td class="am-hide-sm-only">'+data[i].arrivalTime+'</td>\n' +
                            '<td>\n' +
                            '<div class="am-btn-toolbar">\n' +
                            '<div class="am-btn-group am-btn-group-xs">\n';
                               if (data[i].recordCancle==="0" && data[i].isArrival==="0"  && data[i].isOver=="0"){
                                   dom +='<button class="am-btn am-btn-default am-btn-xs am-text-warning lxy_start"><span class="am-icon-lock"></span> 入座</button>';
                                   dom +='<button class="am-btn am-btn-default am-btn-xs am-text-secondary lxy_edit"><span class="am-icon-pencil-square-o"></span> 编辑</button>';
                                   dom += '<button class="am-btn am-btn-default am-btn-xs am-text-danger  lxy_delete"><span class="am-icon-trash-o"></span> 取消</button>\n';
                               }else if (data[i].isArrival=="1"){
                                   dom +='<button class="am-btn am-btn-default am-btn-xs am-text-success lxy_over"><span class="am-icon-check"></span> 结束</button>';
                               }
                          dom+=  '</div>\n' +
                            '</div>\n' +
                            '</td>\n' +
                            '</tr>';
                        $(".tbody").append(dom);
                    }
                    deletefunc();
                    editfunc();
                    finishfunc();
                    startfunc();

                }else {
                    $.alert("请求超时，请稍后再试！");
                }

            }
        });
    }
    function editfunc() {
            $('#doc-modal-list').find('.lxy_edit').add('#doc-confirm-toggle').on('click', function(e) {
                e.preventDefault();
                var id= $(this).parent().parent().parent().parent().attr("class").substr(8,1);
                var $confirm = $('#lxy-confirm');
                var confirm = $confirm.data('amui.modal');
                $("#tableselect").empty();
                $(".peoplenumber").val("");
                $(".yongcantime").val("");
                var onConfirm = function() {
                    var peoplenumber = $(".peoplenumber").val();
                    var arrivaltime = $(".arrivaltime").val();
                    var tableno =$('#tableselect option:selected').val();
                    $.ajax({
                        type: "put",
                        url: "/api/bookingrecord",    //向后端请求数据的url
                        data: {
                            id:id,
                            peoplenum:peoplenumber,
                            tableno:tableno,
                            arrivaltime:arrivaltime,
                            isarrival:0,
                            isover:0,
                            recordcancle:0
                        },
                        success: function (data) {
                            if (data){
                                $.alert("顾客 "+data.customer.name+" 预约信息修改成功");
                                getlist(kindvalue);
                            }else {
                                $.alert("系统错误，预约信息修改失败！");
                            }

                        }
                    });
                };
                var onCancel = function() {
                }

                if (confirm) {
                    confirm.options.onConfirm =  onConfirm;
                    confirm.options.onCancel =  onCancel;
                    confirm.toggle(this);
                } else {
                    $confirm.modal({
                        relatedElement: this,
                        onConfirm: onConfirm,
                        onCancel: onCancel
                    });
                }
            });

        }
     function deletefunc() {
        $('#doc-modal-list').find('.lxy_delete').add('#doc-confirm-toggle').on('click', function(e) {
            e.preventDefault();
            var id= $(this).parent().parent().parent().parent().attr("class").substr(8,1);
            var $confirm = $('#my-confirm');
            var confirm = $confirm.data('amui.modal');
            var onConfirm = function() {
                // alert('你确定删除 ' + id);
                $.ajax({
                    type: "put",
                    url: "/api/bookingrecord/cancel",    //向后端请求数据的url
                    data: {
                        id:id
                    },
                    success: function (data) {
                        if (data){
                            $.alert("取消预约成功！");
                            getlist(kindvalue);
                        }else {
                            $.alert("系统超时，取消失败！");
                        }
                    }
                });
            };
            var onCancel = function() {
            };

            if (confirm) {
                confirm.options.onConfirm =  onConfirm;
                confirm.options.onCancel =  onCancel;
                confirm.toggle(this);
            } else {
                $confirm.modal({
                    relatedElement: this,
                    onConfirm: onConfirm,
                    onCancel: onCancel
                });
            }
        });
        
    }

     function finishfunc() {
            $('#doc-modal-list').find('.lxy_over').add('#doc-confirm-toggle').on('click', function(e) {
                e.preventDefault();
                var id= $(this).parent().parent().parent().parent().attr("class").substr(8,1);
                var $confirm = $('#end-confirm');
                var confirm = $confirm.data('amui.modal');
                var onConfirm = function() {
                    // alert('你确定删除 ' + id);
                    $.ajax({
                        type: "put",
                        url: "/api/bookingrecord/end",    //向后端请求数据的url
                        data: {
                            id:id
                        },
                        success: function (data) {
                            if (data){
                                $.alert("结束用餐成功！");
                                getlist(kindvalue);
                            }else {
                                $.alert("系统超时，结束用餐失败！");
                            }
                        }
                    });
                };
                var onCancel = function() {
                };

                if (confirm) {
                    confirm.options.onConfirm =  onConfirm;
                    confirm.options.onCancel =  onCancel;
                    confirm.toggle(this);
                } else {
                    $confirm.modal({
                        relatedElement: this,
                        onConfirm: onConfirm,
                        onCancel: onCancel
                    });
                }
            });

        }
     function startfunc() {
            $('#doc-modal-list').find('.lxy_start').add('#doc-confirm-toggle').on('click', function(e) {
                e.preventDefault();
                var id= $(this).parent().parent().parent().parent().attr("class").substr(8,1);
                var $confirm = $('#start-confirm');
                var confirm = $confirm.data('amui.modal');
                var onConfirm = function() {
                    $.ajax({
                        type: "put",
                        url: "/api/bookingrecord/start",    //向后端请求数据的url
                        data: {
                            id:id
                        },
                        success: function (data) {
                            if (data){
                                $.alert("入座成功！");
                                getlist(kindvalue);
                            }else {
                                $.alert("系统超时，入座失败！");
                            }
                        }
                    });
                };
                var onCancel = function() {
                };

                if (confirm) {
                    confirm.options.onConfirm =  onConfirm;
                    confirm.options.onCancel =  onCancel;
                    confirm.toggle(this);
                } else {
                    $confirm.modal({
                        relatedElement: this,
                        onConfirm: onConfirm,
                        onCancel: onCancel
                    });
                }
            });

        }


    });

})(jQuery);